from typing import List

"""
55. 跳跃游戏
https://leetcode.cn/problems/jump-game/description/?envType=study-plan-v2&envId=top-interview-150
"""


class Solution:
    """
    贪心
    """
    def canJump(self, nums: List[int]) -> bool:
        n, rightmost = len(nums), 0
        for i in range(n):
            if i <= rightmost:
                rightmost = max(rightmost, i + nums[i])
                if rightmost >= n - 1:
                    return True
        return False

    """
    倒序
    """
    def canJump2(self, nums: List[int]) -> bool:
        n = len(nums)
        steps = n - 1 #需要步数
        for i in range(n - 2, -1, -1):
            var = nums[i]
            var2 = steps - i
            if nums[i] >= steps - i:
                steps = i
        return steps == 0

if __name__ == '__main__':
    s = Solution()
    print(s.canJump([2, 3, 0, 1, 0]))
